package me.panpf.sketch.sample.widget.fraction;

import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.util.ResUtil;
import me.panpf.sketch.sample.ResourceTable;

import me.panpf.sketch.sample.widget.page.*;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.List;

public class ImageProcessorTestFraction extends Fraction {
    private DirectionalLayout mDlViewRoot;
    private PageSlider mPager;
    private TabPageIndicator mTabIndicator;
    private List<AbstractPageView> mPageViews;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_image_processor_test_fraction, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        mDlViewRoot = (DirectionalLayout) getComponent().findComponentById(ResourceTable.Id_dlRootLayout);
        mPager = (PageSlider) getComponent().findComponentById(ResourceTable.Id_slider);
        mTabIndicator = (TabPageIndicator) getComponent().findComponentById(ResourceTable.Id_tlTabLayout);
        initPageView(intent);
        initTabs();
    }

    private void initPageView(Intent intent) {
        initPager();
        mPager.setOrientation(Component.HORIZONTAL);
        mPager.setSlidingPossible(true);
        mDlViewRoot.setBackground(ResUtil.buildDrawableByColor(ResourceTable.Color_color_ability_bg));
        mPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int position) {
                mTabIndicator.selectTab(mTabIndicator.getTabAt(position));
            }
        });
    }

    private void initTabs() {
        mTabIndicator.setTabTextSize(34);
        mTabIndicator.setTabMargin(18);
        mTabIndicator.setOrientation(Component.HORIZONTAL);
        mTabIndicator.setCentralScrollMode(true);
        mTabIndicator.setTabTextAlignment(TextAlignment.CENTER);
        mTabIndicator.setTabTextColors(getFractionAbility().getContext().getColor(ResourceTable.Color_color_tab_normal),
                Color.WHITE.getValue());
        mTabIndicator.setSelectedTabIndicatorHeight(0);
        mTabIndicator.setViewPager(mPager);
        mTabIndicator.setFixedMode(false);
        mTabIndicator.selectTab(mTabIndicator.getTabAt(0));
    }

    private void initPager() {
        mPageViews = new ArrayList<>();
        mPageViews.add(new ReflectionImageProcessorTestPageView(getComponent().getContext(), "REFLECTION"));
        mPageViews.add(new GaussianBlurImageProcessorTestPageView(getComponent().getContext(), "GAUSSIAN_BLUR"));
        mPageViews.add(new RotateImageProcessorTestPageView(getComponent().getContext(), "ROTATE"));
        mPageViews.add(new RoundRectImageProcessorTestPageView(getComponent().getContext(), "ROUND_RECT"));
        mPageViews.add(new CircleImageProcessorTestPageView(getComponent().getContext(), "CIRCLE"));
        mPageViews.add(new ResizeImageProcessorTestPageView(getComponent().getContext(), "RESIZE"));
        mPageViews.add(new MaskImageProcessorTestPageView(getComponent().getContext(), "MASK"));
        mPageViews.add(new WrappedImageProcessorTestPageView(getComponent().getContext(), "WRAPPED"));
        PageViewAdapter pageViewAdapter = new PageViewAdapter(mPageViews);
        mPager.setProvider(pageViewAdapter);
    }
}
